package com.codi.api.controller;

import com.codi.api.security.UserUtil;
import com.codi.base.exception.BaseAppException;
import com.codi.base.log.annotation.Loggable;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.service.OptionalService;
import com.codi.bus.core.vo.OptionalFundVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by asus on 2017/12/6.
 * 自选收藏模块
 */
@Slf4j
@RestController
@RequestMapping("/optional")
public class OptionalController {
    @Autowired
    OptionalService optionalService;

    /**
     * 添加组合自选
     *
     * @param portfolioCode
     * @return
     */
    @Loggable
    @PostMapping("/addPortfolio")
    public BaseResult addPortfolio(String portfolioCode, String portfolioName) {
        return optionalService.addPortfolio(portfolioCode, portfolioName, UserUtil.getLoginUserId());
    }

    /**
     * 添加基金 自选
     */
    @Loggable
    @PostMapping("/addFund")
    public BaseResult addFund(@Validated OptionalFundVo fundOptional) throws BaseAppException {
        if (fundOptional.getFundTypeCode() == 1109) {
            return optionalService.addCurrency(fundOptional, UserUtil.getLoginUserId());
        } else {
            return optionalService.addFund(fundOptional, UserUtil.getLoginUserId());
        }
    }

    /**
     * 查询组合自选
     *
     * @return
     */
    @Loggable
    @GetMapping("/portfolio")
    public BaseResult getPortfolio() {
        return optionalService.queryPortfolio(UserUtil.getLoginUserId());
    }

    /**
     * 查询普通基金自选
     *
     * @return
     */
    @GetMapping("/fund")
    public BaseResult getFund() {
        return optionalService.queryFund(UserUtil.getLoginUserId());
    }

    /**
     * 查询货币基金自选
     *
     * @return
     */
    @Loggable
    @GetMapping("/currency")
    public BaseResult getCurrency() {
        return optionalService.queryCurrency(UserUtil.getLoginUserId());
    }

    /**
     * 对组合自选进行排序删除操作
     *
     * @param portfolioOptional
     * @return
     */
    @Loggable
    @PostMapping("/orderPortfolio")
    public BaseResult orderPortfolio(String portfolioOptional) {
        return optionalService.orderPortfolio(portfolioOptional, UserUtil.getLoginUserId());
    }

    /**
     * 对普通基金进行排序删除操作
     *
     * @param fundOptional
     * @return
     */
    @Loggable
    @PostMapping("/orderFund")
    public BaseResult orderFund(String fundOptional) {
        return optionalService.orderFund(fundOptional, UserUtil.getLoginUserId(), false);
    }

    /**
     * 对货币基金进行排序删除操作
     *
     * @param currencyOptional
     * @return
     */
    @Loggable
    @PostMapping("/orderCurrency")
    public BaseResult orderCurrency(String currencyOptional) {
        return optionalService.orderFund(currencyOptional, UserUtil.getLoginUserId(), true);
    }

    /**
     * 组合 普通基金 货币基金自选 的排序和删除
     *
     * @param portfolioOptional
     * @param fundOptional
     * @param currencyOptional
     * @return
     */
    @Loggable
    @PostMapping("/order")
    public BaseResult order(String portfolioOptional, String fundOptional, String currencyOptional) {
        optionalService.orderPortfolio(portfolioOptional, UserUtil.getLoginUserId());
        optionalService.orderFund(fundOptional, UserUtil.getLoginUserId(), false);
        optionalService.orderFund(currencyOptional, UserUtil.getLoginUserId(), true);
        return new BaseResult();
    }

    /**
     * 查看组合是否已添加自选
     */
    @Loggable
    @GetMapping("/isAddPortfolio")
    public BaseResult isAddPortfolio(String portfolioCode) {
        return optionalService.isAddPortfolio(portfolioCode, UserUtil.getLoginUserId());
    }

    /**
     * 查看基金是否已添加自选
     */
    @Loggable
    @GetMapping("/isAddFund")
    public BaseResult isAddFund(String fundCode) {
        return optionalService.isAddFund(fundCode, UserUtil.getLoginUserId());
    }

    /**
     * 删除组合自选
     *
     * @return
     */
    @Loggable
    @PostMapping("deletePortfolio")
    public BaseResult delete(String portfolioCode) {
        return optionalService.deletePortfolio(portfolioCode, UserUtil.getLoginUserId());
    }

    /**
     * 删除组合自选
     *
     * @return
     */
    @Loggable
    @PostMapping("deleteFund")
    public BaseResult delete(String fundCode, int fundTypeCode) {
        return optionalService.deleteFund(fundCode, UserUtil.getLoginUserId(), fundTypeCode);
    }
}
